package cn.iocoder.yudao.module.product.controller.admin.packaging;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.packaging.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.packaging.PackagingDO;
import cn.iocoder.yudao.module.product.service.packaging.PackagingService;

@Tag(name = "管理后台 - 商品包装物")
@RestController
@RequestMapping("/product/packaging")
@Validated
public class PackagingController {

    @Resource
    private PackagingService packagingService;

    @PostMapping("/create")
    @Operation(summary = "创建商品包装物")
    @PreAuthorize("@ss.hasPermission('product:packaging:create')")
    public CommonResult<Integer> createPackaging(@Valid @RequestBody PackagingSaveReqVO createReqVO) {
        return success(packagingService.createPackaging(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品包装物")
    @PreAuthorize("@ss.hasPermission('product:packaging:update')")
    public CommonResult<Boolean> updatePackaging(@Valid @RequestBody PackagingSaveReqVO updateReqVO) {
        packagingService.updatePackaging(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品包装物")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:packaging:delete')")
    public CommonResult<Boolean> deletePackaging(@RequestParam("id") Integer id) {
        packagingService.deletePackaging(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品包装物")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:packaging:query')")
    public CommonResult<PackagingRespVO> getPackaging(@RequestParam("id") Integer id) {
        PackagingDO packaging = packagingService.getPackaging(id);
        return success(BeanUtils.toBean(packaging, PackagingRespVO.class));
    }
    @GetMapping("/get/skuId")
    @Operation(summary = "通过Skuid获得商品包装物")
    @Parameter(name = "skuId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:packaging:query')")
    public CommonResult<PackagingRespVO> getPackagingBySkuId (@RequestParam("skuId") Long skuId) {
        PackagingDO packaging = packagingService.getPackagingBySkuId(skuId);
        return success(BeanUtils.toBean(packaging, PackagingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品包装物分页")
    @PreAuthorize("@ss.hasPermission('product:packaging:query')")
    public CommonResult<PageResult<PackagingRespVO>> getPackagingPage(@Valid PackagingPageReqVO pageReqVO) {
        PageResult<PackagingDO> pageResult = packagingService.getPackagingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PackagingRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得商品包装物列表")
    @PreAuthorize("@ss.hasPermission('product:packaging:query')")
    public CommonResult<List<PackagingRespVO>> getPackagingList() {
        List<PackagingDO> pageResult = packagingService.getPackagingList();
        return success(BeanUtils.toBean(pageResult, PackagingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品包装物 Excel")
    @PreAuthorize("@ss.hasPermission('product:packaging:export')")
    @OperateLog(type = EXPORT)
    public void exportPackagingExcel(@Valid PackagingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PackagingDO> list = packagingService.getPackagingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品包装物.xls", "数据", PackagingRespVO.class,
                        BeanUtils.toBean(list, PackagingRespVO.class));
    }

}